package com.jctrip.hotel.source.xxl.elong.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.model.elong.req.offline.ElongQueryHotelListReq;
import com.jctrip.hotel.source.service.elong.ElongTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-25 17:46
 * @Description:
 */
@Slf4j
public class SyncElongHotelInfoThread implements Runnable {
    private final String secretId;
    private final String secretKey;
    private final String user;
    private final String local;

    public SyncElongHotelInfoThread(String user, String secretId, String secretKey, String local) {
        this.secretId = secretId;
        this.secretKey = secretKey;
        this.user = user;
        this.local = local;
    }

    @Override
    public void run() {
        try {
            RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
            ElongTaskService elongTaskService = BeanContext.getApplicationContext().getBean(ElongTaskService.class);
            while (redisUtil.lGetListSize(RedisKeyConstant.SYNC_MAIN_ELONG_HOTELId_LIST) > 0) {
                try {
                    String cityCode = (String) redisUtil.rightPop(RedisKeyConstant.SYNC_MAIN_ELONG_HOTELId_LIST);
                    if (StringUtils.isBlank(cityCode)) {
                        continue;
                    }
                    elongTaskService.syncHotelInfoTask(user, secretId, secretKey, local,
                            ElongQueryHotelListReq.builder()
                                    .cityId(cityCode)
                                    .pageIndex(1)
                                    .pageSize(2000)
                                    .build());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
